/*
 * @Author: qinuoyun
 * @Date:   2021-09-17 21:34:56
 * @Last Modified by:   qinuoyun
 * @Last Modified time: 2021-10-08 22:27:34
 */
import axios from 'axios';

export default function server(options) {
    //设置初始化URL
    axios.defaults.baseURL = options.AppURL;
    //开启Session存储
    axios.defaults.withCredentials = true;
    //此时超时配置的默认值是 `0`
    let cloud = axios.create();
    //现在，在超时前，所有请求都会等待 2.5 秒
    cloud.defaults.timeout = 1000000;

    cloud.interceptors.request.use(
        config => {
            let token = localStorage.getItem('userToken') || '';
            if (token) {
                config.headers['Authorization'] = 'Bearer ' + token;
            }
            return config;
        },
        error => {
            return Promise.reject(error);
        }
    );

    cloud.interceptors.response.use(
        response => {
            if (response.status >= 200 && response.status < 300) {
                if (response.headers['x-pagination-total-count'] || response.headers['X-Pagination-Total-Count']) {
                    return response;
                }
                return response.data;
            } else {
                return Promise.reject(response);
            }
        },
        async (error) => {
            return Promise.reject(error.response);
        }
    );
    return cloud;
}
